<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <base href="${APP_PATH}/">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>教学管理系统</title>
    <link rel="icon" href="public/favicon.ico" type="image/ico">
    <%@include file="/commons.jsp" %>
</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <%@ include file="/menu.jsp" %>
        <!--End 左侧导航-->
        <!--头部信息-->
        <%@ include file="/head.jsp" %>
        <!--End 头部信息-->
        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <form class="pull-right search-bar" id="search" method="post" action="/manage/user/indexDo">
                                    <div class="input-group">
                                        <div class="input-group-btn">
                                            <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                                                角色 <span class="caret"></span>
                                            </button>
                                            <ul class="dropdown-menu" id="role">
                                                <li><a href="javascript:void(0)">角色</a></li>
                                                <li><a href="javascript:void(0)">学生</a></li>
                                                <li><a href="javascript:void(0)">班主任</a></li>
                                                <li><a href="javascript:void(0)">教师</a></li>
                                                <li><a href="javascript:void(0)">就业专员</a></li>
                                                <li><a href="javascript:void(0)">公司</a></li>
                                                <li><a href="javascript:void(0)">教学主管</a></li>
                                                <li><a href="javascript:void(0)">管理员</a></li>
                                            </ul>
                                        </div>
                                        <input type="text" class="form-control" name="condition" placeholder="请输入查询条件"/>
                                        <div class="input-group-btn">
                                            <button class="btn btn-default dropdown-toggle" id="searchBtn" type="button">搜索</button>
                                        </div>
                                    </div>
                                </form>
                                <div class="toolbar-btn-action">
                                    <a class="btn btn-primary m-r-5" href="/manage/user/add"><i class="mdi mdi-plus"></i> 新增</a>
                                    <a class="btn btn-danger" onclick="delAll()"><i class="mdi mdi-window-close"></i> 删除</a>
                                </div>
                            </div>
                            <div class="card-body">
                                <form id="dataForm">
                                    <div class="table-responsive">
                                        <table class="table table-bordered">
                                            <thead>
                                            <tr>
                                                <th>
                                                    <label class="lyear-checkbox checkbox-primary">
                                                        <input type="checkbox" id="check-all"><span></span>
                                                    </label>
                                                </th>
                                                <th>序号</th>
                                                <th>编号</th>
                                                <th>名字</th>
                                                <th>密码</th>
                                                <th>邮箱</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody id="dataList">
                                            </tbody>
                                        </table>
                                        <div align="center">
                                            <ul class="pagination" id="pages"></ul>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>
<script type="text/javascript">
    $(function () {
        layer.msg("加载中", {time: 200, icon: 16, shift: 5}, function () {
            goPage(1);
        });//layer
        // 下拉事件
        $('.search-bar .dropdown-menu a').click(function () {
            $('#search-btn').html($(this).text() + ' <span class="caret"></span>');
        });
    });//入口

    function assignRole(uid) {
        location.href = "manage/role/assignRole?uid=" + uid;
    }

    function del(uid) {
        if (confirm("确认删除吗？")) {
            $.ajax({
                url: "manage/user/del",
                data: {"uid": uid},
                type: "post",
                success: function (obj) {
                    if (obj.result) {
                        layer.msg("删除用户成功", {time: 1000, icon: 6, shift: 6}, function () {
                            location.href = "manage/user/index"
                        })
                    } else {
                        layer.msg("删除用户失败", {time: 1000, icon: 5, shift: 6}, function () {
                        })
                    }
                }
            })
        }
    }

    function delAll() {
        if (confirm("确认删除吗？")) {
            $.ajax({
                url: "manage/user/delAll",
                data: $("#dataForm").serialize(),
                type: "post",
                success: function (obj) {
                    if (obj.result) {
                        layer.msg("删除用户成功", {time: 1000, icon: 6, shift: 6}, function () {
                            location.href = "manage/user/index"
                        })
                    } else {
                        layer.msg("删除用户失败", {time: 1000, icon: 5, shift: 6}, function () {
                        })
                    }
                }
            })
        }
    }

    function goPage(pageNum) {
        let rnamVal = $('#search-btn').text().trim();
        $.ajax({
            url: "manage/user/indexDo?pageNum=" + pageNum + "&rname=" + rnamVal,
            type: "post",
            data: $("#search").serialize(),
            dataType: "json",
            success: function (obj) {
                if (obj.result) {
                    let dataHtml = template("dataTpl", {
                        users: obj.userList
                    });
                    $("#dataList").html(dataHtml);
                    let prePage = obj.pageInfo.pageNum == 1 ? 1 : obj.pageInfo.pageNum - 1;
                    let nextPage = obj.pageInfo.pageNum == obj.pageInfo.pages ? obj.pageInfo.pages : obj.pageInfo.pageNum + 1;
                    var pageMessage = "";
                    pageMessage += '<li class="' + (obj.pageInfo.pageNum == 1 ? "disabled" : "") + '"><a onclick="goPage(' + prePage + ')"><<</a></li>';
                    for (var i = 1; i <= obj.pageInfo.pages; i++) {
                        pageMessage += '<li class="' + (obj.pageInfo.pageNum == i ? "active" : "") + '"><a onclick="goPage(' + i + ')">' + i + ' <span class="sr-only">(current)</span></a></li>';
                    }
                    pageMessage += '<li class="' + (obj.pageInfo.pageNum == obj.pageInfo.pages ? "disabled" : "") + '"><a onclick="goPage(' + nextPage + ')">>></a></li>';
                    $("#pages").html(pageMessage);
                    $("#searchBtn").unbind();
                    $("#searchBtn").click(function () {
                        goPage(obj.pageInfo.pageNum);
                    })

                }
            }//success
        });//ajax
    }
</script>
</body>
</html>
<script id="dataTpl" type="text/html">
    {{each users user index}}
    <tr>
        <td>
            <label class="lyear-checkbox">
                <input type="checkbox" name="uids" value="{{user.uid}}"><span></span>
            </label>
        </td>
        <td>{{index+1}}</td>
        <td>{{user.sno}}</td>
        <td>{{user.uname}}</td>
        <td>{{user.upwd}}</td>
        <td>{{user.ueamail}}</td>
        <td>
            <div class="btn-group">
                <a class="btn btn-xs btn-default" onclick="assignRole('{{user.uid}}')" title="修改角色" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                <a class="btn btn-xs btn-default" onclick="del('{{user.uid}}')" title="删除用户" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>
            </div>
        </td>
    </tr>
    {{/each}}
</script>
